package com.runda.partypolicy.app.page.fragment.home;


import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.runda.partypolicy.R;
import com.runda.partypolicy.app.base.BaseFragment;
import com.runda.partypolicy.app.page.adapter.RedPagerAdapter;
import com.runda.partypolicy.app.page.fragment.task.Fragment_Task;
import com.runda.partypolicy.app.page.viewmodel.ViewModel_Main_Task;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;


/**
 * 主页—任务
 */
public class Fragment_Main_Task extends BaseFragment<ViewModel_Main_Task> {
    @BindView(R.id.tablayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.commen_title_name)
    TextView commenTitleName;
    @BindView(R.id.commen_title_img)
    ImageView commenTitleImg;
    @BindView(R.id.commen_title_back)
    ImageView commenTitleBack;
    @BindView(R.id.commen_title_start)
    ImageView commenTitleStart;
    @BindView(R.id.commen_search_img)
    ImageView commenSearchImg;
    private ArrayList<Fragment> fragments;
    private String[] titles = new String[]{"待办任务", "我交办的任务", "便捷请示"};
    private RedPagerAdapter pagerAdapter;
    private int currentId;


    public Fragment_Main_Task() {
        // Required empty public constructor
    }

    public static Fragment_Main_Task newInstance() {

        Bundle args = new Bundle();

        Fragment_Main_Task fragment = new Fragment_Main_Task();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_main_task;
    }

    @Override
    public ViewModel_Main_Task initViewModel() {
        return ViewModelProviders.of(this,getViewModelFactory()).get(ViewModel_Main_Task.class);
    }

    @Override
    public void initEvents() {

    }

    @Override
    public void start() {
        commenSearchImg.setVisibility(View.VISIBLE);

        commenTitleName.setText("任务");
        initTablayout();
    }

    @Override
    public void initNotSignEvent() {

    }

    @Override
    public void initNoNetworkEvent() {

    }

    @Override
    public void initShowMessageEvent() {

    }

    @Override
    public void initTokenOverTimeEvent() {

    }

    @Override
    public void initShowOrDismissWaitingEvent() {

    }

    @Override
    public void initStateLayoutEvent() {

    }

    private void initTablayout() {

        fragments = new ArrayList<>();
        fragments.add(Fragment_Task.newInstance("待办"));
        tabLayout.addTab(tabLayout.newTab());
        fragments.add(Fragment_Task.newInstance("交办"));
        tabLayout.addTab(tabLayout.newTab());
        fragments.add(Fragment_Task.newInstance("便捷"));
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.setupWithViewPager(viewPager, false);

        pagerAdapter = new RedPagerAdapter(getChildFragmentManager(), fragments, titles, getActivity());
        viewPager.setAdapter(pagerAdapter);
        setRedPoint();
        if (fragments.size() > 0) {
            for (int i = 0; i < titles.length; i++) {
                tabLayout.getTabAt(i).setText(titles[i]);
            }
        }

    }

    private void setRedPoint() {
        //设置小红点
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            View tabView = pagerAdapter.getTabView(i);
            TextView tv_num = (TextView) tabView.findViewById(R.id.iv_tab_red);

            if (i == 1) {
                tv_num.setText("15");
            } else {
                tv_num.setVisibility(View.GONE);
            }

            /**在这里判断每个TabLayout的内容是否有更新，来设置小红点是否显示**/

            tabLayout.getTabAt(i).setCustomView(tabView);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("Fragment_Main_Task", "onResume==");
        tabLayout.getTabAt(currentId).select();

    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);

        currentId = tabLayout.getSelectedTabPosition();
    }




}
